package com.dgy.teachResource.service.impl;

import com.alibaba.fastjson2.JSON;
import com.dgy.teachResource.pojo.dto.SelectTeachRecordDTO;
import com.dgy.teachResource.pojo.vo.TeachRecordVO;
import com.dgy.teachResource.service.ITeachRecordService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.JsonUtil;
import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 授课记录Service业务层处理
 *
 * @author ccc
 * @date 2025-02-25
 */
@Service
public class TeachRecordServiceImpl implements ITeachRecordService
{
    private final Log logger = LogFactory.getLog(getClass());

    @Autowired
    @Qualifier("customRestTemplate")
    private RestTemplate restTemplate;

    /**
     * 教学资源平台请求url
     */
    private String url = "http://222.133.48.34:88";

    /**
     * 获取上课记录
     *
     * @param selectTeachRecordDTO
     * @return
     */
    @Override
    public TableDataInfo selectTeachRecord(SelectTeachRecordDTO selectTeachRecordDTO)
    {
        // 请求地址
        String requestUrl = url + "/e/action/indexwapAction.php?t=getskjllist&page={page}&keyword={keyword}";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", selectTeachRecordDTO.getPage());
//        if(StringUtils.isNotBlank(selectTeachRecordDTO.getKeyword())){
//            selectTeachRecordDTO.setKeyword(URLEncoder.encode(selectTeachRecordDTO.getKeyword()));
//        }
        paramMap.put("keyword", selectTeachRecordDTO.getKeyword());
        this.logger.info("请求教学资源平台获取上课记录,请求参数:{}"+JSON.toJSONString(paramMap));
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(requestUrl, String.class, paramMap);
        String responseBody = responseEntity.getBody();
        // 去除 BOM 字符
        responseBody = JsonUtil.removeBOM(responseBody);
        this.logger.info("请求教学资源平台获取上课记录,返参:"+ responseBody);

        // 将 JSON 字符串转换为 Java 对象（这里使用 Map 来表示 JSON 对象）
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> jsonObject = null;
        try {
            jsonObject = objectMapper.readValue(responseBody, Map.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        // 读取字段
        int total = (int) jsonObject.get("total");
        Object data = jsonObject.get("data");
        List<TeachRecordVO> teachRecordVOList = JSON.parseArray(JSON.toJSONString(data), TeachRecordVO.class);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(total);
        tableDataInfo.setRows(teachRecordVOList);
        return tableDataInfo;
    }

    @Override
    public TeachRecordVO getTeachRecordInfo(String teachRecordId) {
        // 请求地址
        String requestUrl = url + "/e/action/indexwapAction.php?t=getskjlbyid&id={teachRecordId}";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("teachRecordId", teachRecordId);
        this.logger.info("请求教学资源平台获取上课详情,请求参数:{}"+JSON.toJSONString(paramMap));
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(requestUrl, String.class, paramMap);
        String responseBody = responseEntity.getBody();
        // 去除 BOM 字符
        responseBody = JsonUtil.removeBOM(responseBody);
        this.logger.info("请求教学资源平台获取上课详情,返参:"+ responseBody);

        // 将 JSON 字符串转换为 Java 对象（这里使用 Map 来表示 JSON 对象）
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> jsonObject = null;
        try {
            jsonObject = objectMapper.readValue(responseBody, Map.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        // 读取数据
        Object data = jsonObject.get("data");
        if(ObjectUtils.isEmpty(data)){
            return null;
        }
        TeachRecordVO teachRecordVO = JSON.parseObject(JSON.toJSONString(data), TeachRecordVO.class);
        return teachRecordVO;
    }

}
